import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'


const routes = [
  {
    path: '/',
    name: 'Layout',
    component:Layout,
    redirect:'/index',
    meta: {
      requireAuth: false
    },
    children:[
      {
        path:'index',
        name: 'home',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: '首页',
          requireAuth: false
        }
      },
      {
        path: '/about',
        name: 'About',
        component: () => import('@/views/about/index.vue'),
        meta: {
          title: '关于我们'
        }
      },
      {
        path: '/user',
        name: 'userCenter',
        component: () => import('../views/user/index.vue'),
        meta: {
          title: '用户中心',
          requireAuth: true
        },
        children:[
          {
            path: 'feedback/survey',
            name: 'survey',
            meta: {
              title: '问卷调查',
              icon:''
            },
            component: () => import('@/views/user/feedback/survey/index.vue')
          },
          {
            path: 'feedback/day',
            name: 'day',
            meta: {
              title: '每日反馈',
              icon:''
            },
            component: () => import('@/views/user/feedback/day/index.vue')
          },
          {
            path: 'feedback/complaint',
            name: 'complaint',
            meta: {
              title: '投诉建议',
              icon:''
            },
            component: () => import('@/views/user/feedback/complaint/index.vue')
          },
        ]
      },
    ]
  },


  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      requireAuth: false
    }
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import('@/views/reg/index.vue'),
    meta: {
      title: '注册',
      requireAuth: false
    }
  }
]


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!localStorage.getItem('token') // 假设 token 存储在 localStorage 中

  // if (to.meta.requireAuth && !isAuthenticated) {
  //   next({ name: 'login', query: { redirect: to.fullPath } })
  // } else {
  //   next()
  // }
  next();
})

export default router
